Passed
Push — master ( 169dfd...f76276 )
by Stiofan
04:33
created

gaaf.js ➔ ... ➔ fillInAddress   D

Complexity

Conditions 10
Paths 10

Size

Total Lines 100

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 10
nc 10
nop 0
dl 0
loc 100
rs 4.8196
c 2
b 0
f 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like gaaf.js ➔ ... ➔ fillInAddress often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
jQuery(function ($) {
2
    var autocomplete = new google.maps.places.Autocomplete(
0 ignored issues
show
Bug introduced by
The variable google seems to be never declared. If this is a global, consider adding a /** global: google */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
3
        document.getElementById('wpinv_address'),
4
        {types: ['geocode']}
5
    );
6
    autocomplete.addListener('place_changed', fillInAddress);
7
    function fillInAddress() {
8
        var place = autocomplete.getPlace();
9
        var temp_state = '';
10
        
11
        for (var i = 0; i < place.address_components.length; i++) {
12
13
            var address_type = place.address_components[i].types[0];
14
15
            switch (address_type) {
16
                case 'route':
17
                    var val = place.address_components[i]['short_name'];
18
                    document.getElementById('wpinv_address').value = val;
19
                    break;
20
                case 'locality':
21
                    var val = place.address_components[i]['long_name'];
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable val already seems to be declared on line 17. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
22
                    document.getElementById('wpinv_city').value = val;
23
                    break;
24
                case 'administrative_area_level_1':
25
                    var val = place.address_components[i]['short_name'];
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable val already seems to be declared on line 17. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
26
                    temp_state = val;
27
                    break;
28
                case 'country':
29
                    var val = place.address_components[i]['short_name'];
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable val already seems to be declared on line 17. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
30
31
                    $('#wpinv_country').val(val);
32
33
                    var elB = $('#wpinv-address');
34
                    var elF = $('#wpinv-fields');
35
                    data = {
0 ignored issues
show
Bug introduced by
The variable data seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.data.
Loading history...
36
                        action: 'wpinv_get_states_field',
37
                        country: val,
38
                        field_name: 'wpinv_state',
39
                    };
40
41
                    if(elB.length){
42
                        var $this = $('#wpinv_country', elB);
43
44
                        $this.closest('.gdmbx-row').find('.wpi-loader').show();
45
                        $('#wpinv_state', elB).css({
46
                            'opacity': '.5'
47
                        });
48
                        $.post(ajaxurl, data, function(response) {
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
49
                            if ('nostates' === response || '' == temp_state) {
0 ignored issues
show
Bug introduced by
The variable temp_state is changed as part of the for loop for example by val on line 26. Only the value of the last iteration will be visible in this function if it is called after the loop.
Loading history...
50
                                var text_field = '<input type="text" value="' + temp_state + '" id="wpinv_state" name="wpinv_state" />';
51
                                $('#wpinv_state', elB).replaceWith(text_field);
0 ignored issues
show
Bug introduced by
The variable elB is changed as part of the for loop for example by $("#wpinv-address") on line 33. Only the value of the last iteration will be visible in this function if it is called after the loop.
Loading history...
52
                            } else {
53
                                $('#wpinv_state', elB).replaceWith(response);
54
                                $('#wpinv_state', elB).find('option[value="' + temp_state + '"]').attr('selected', 'selected');
55
                                $('#wpinv_state', elB).find('option[value=""]').remove();
56
                            }
57
58
                            $('#wpinv_state', elB).addClass('gdmbx2-text-large');
59
                            if (typeof $this.data('change') === '1') {
0 ignored issues
show
Bug introduced by
The variable $this is changed as part of the for loop for example by $("#wpinv_country", elB) on line 42. Only the value of the last iteration will be visible in this function if it is called after the loop.
Loading history...
60
                                $('#wpinv_state', elB).change();
61
                            } else {
62
                                window.wpiConfirmed = true;
63
                                $('#wpinv-recalc-totals').click();
64
                                window.wpiConfirmed = false;
65
                            }
66
67
                            $this.closest('.gdmbx-row').find('.wpi-loader').hide();
68
                            $('#wpinv_state', elB).css({
69
                                'opacity': '1'
70
                            });
71
                        });
72
                    } else if(elF.length){
73
                        $('.wpinv_errors').remove();
74
                        wpinvBlock(jQuery('#wpinv_state_box'));
75
                        var $this = $('#wpinv_country', elF);
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable $this already seems to be declared on line 42. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
76
                        $.post(ajaxurl, data, function(response) {
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
77
                            if ('nostates' === response) {
78
                                var text_field = '<input type="text" required="required" class="wpi-input required" id="wpinv_state" name="wpinv_state">';
79
                                $('#wpinv_state', elF).replaceWith(text_field);
0 ignored issues
show
Bug introduced by
The variable elF is changed as part of the for loop for example by $("#wpinv-fields") on line 34. Only the value of the last iteration will be visible in this function if it is called after the loop.
Loading history...
80
                            } else {
81
                                $('#wpinv_state', elF).replaceWith(response);
82
                                $('#wpinv_state', elF).find('option[value="' + temp_state + '"]').attr('selected', 'selected');
0 ignored issues
show
Bug introduced by
The variable temp_state is changed as part of the for loop for example by val on line 26. Only the value of the last iteration will be visible in this function if it is called after the loop.
Loading history...
83
                                var changeState = function() {
84
                                    wpinv_recalculate_taxes(temp_state);
0 ignored issues
show
Bug introduced by
The variable temp_state is changed as part of the for loop for example by val on line 26. Only the value of the last iteration will be visible in this function if it is called after the loop.
Loading history...
85
                                };
86
                                $("#wpinv_state", elF).unbind("change", changeState);
0 ignored issues
show
Bug introduced by
The variable elF is changed as part of the for loop for example by $("#wpinv-fields") on line 34. Only the value of the last iteration will be visible in this function if it is called after the loop.
Loading history...
87
                                $("#wpinv_state", elF).bind("change", changeState);
88
                            }
89
                            $('#wpinv_state', elF).find('option[value=""]').remove();
90
                            $('#wpinv_state', elF).addClass('form-control wpi-input required');
91
                        }).done(function(data) {
0 ignored issues
show
Unused Code introduced by
The parameter data is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
92
                            jQuery('#wpinv_state_box').unblock();
93
                            wpinv_recalculate_taxes();
94
                        });
95
                    }
96
97
                    break;
98
                case 'postal_code':
99
                    var val = place.address_components[i]['short_name'];
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable val already seems to be declared on line 17. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
100
                    document.getElementById('wpinv_zip').value = val;
101
                    break;
102
                default:
103
                    break;
104
            }
105
        }
106
    }
107
});